/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package au.edu.swin.unilearn.tutorialGroup;

//import au.edu.swin.unilearn.students.Student;
//import au.edu.swin.unilearn.students.StudentEntity;
import au.edu.swin.unilearn.subject.Subject;
import au.edu.swin.unilearn.subject.SubjectEntity;
import au.edu.swin.unilearn.universitystaff.UniversityStaff;
import au.edu.swin.unilearn.universitystaff.UniversityStaffEntity;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author DidierLeung
 */
@Stateless
public class TutorialGroupSessionBean implements TutorialGroupSessionRemote
{
    @PersistenceContext
    private EntityManager em;

    public void addTutorialGroup(TutorialGroup tg)
    {
        TutorialGroupEntity tgEntity = new TutorialGroupEntity();
        tgEntity.setTutorialGroupEntityData(tg);
        UniversityStaff us = tg.getUniversityStaff();
        UniversityStaffEntity use = em.find(UniversityStaffEntity.class, us.getId());
        tgEntity.setUniversityStaffEntity(use);
        Subject s = tg.getSubject();
        SubjectEntity se = em.find(SubjectEntity.class, s.getSubject_code());
        tgEntity.setSubjectEntity(se);
        em.persist(tgEntity);
    }

    public List<TutorialGroup> getAllTutorialGroups()
    {
        List<TutorialGroup> results = new ArrayList<TutorialGroup>();
        Query allTGQuery = em.createNamedQuery("findAllTutorialGroups");
        List dbResults = allTGQuery.getResultList();
        for (int i = 0; i < dbResults.size(); i++)
        {
            TutorialGroupEntity assEntity = (TutorialGroupEntity) dbResults.get(i);
            results.add(assEntity.getTutorialGroupObject());
        }
        return results;
    }

    public TutorialGroup getTutorialGroup(Integer activity_no, String subject_code)
    {
        TutorialGroupPK pk = new TutorialGroupPK(activity_no, subject_code);
        TutorialGroupEntity tgEntity = em.find(TutorialGroupEntity.class, pk);
        return tgEntity.getTutorialGroupObject();
    }

    public List<TutorialGroup> getTutorialGroupOf(String staff_id)
    {
        List<TutorialGroup> results = new ArrayList<TutorialGroup>();
        List<TutorialGroup> allResults = getAllTutorialGroups();
        for (int i = 0; i < allResults.size(); i++)
        {
            TutorialGroup tg = allResults.get(i);
            UniversityStaff us = tg.getUniversityStaff();
            if(staff_id.equals(us.getId()))
            {
                results.add(tg);
            }
        }
        return results;
    }
    
    public Integer getActivityNo(String staff_id, String subject_code)
    {
        List<TutorialGroup> allTutorialGroup = getTutorialGroupOf(staff_id);
        for(TutorialGroup tg : allTutorialGroup)
        {
            Subject s = tg.getSubject();
            if(subject_code.equals(s.getSubject_code()))
            {
                return tg.getActivity_no();
            }
        }
        return null;
    }
//    public void addStudentEntity(StudentEntity se, Integer activity_no, String subject_code)
//    {
////        TutorialGroupPK pk = new TutorialGroupPK(activity_no, subject_code);
////        TutorialGroupEntity tge = em.find(TutorialGroupEntity.class, pk);
////        List<StudentEntity> enrolledStudentEntity = tge.getStudentEntityCollection();
////        enrolledStudentEntity.add(se);
//    }

//    public List<Student> getAllStudent(Integer activity_no, String subject_code)
//    {
////        TutorialGroupPK pk = new TutorialGroupPK(activity_no, subject_code);
////        TutorialGroupEntity tge = em.find(TutorialGroupEntity.class, pk);
////        List<Student> enrolledStudents = new ArrayList();
////        List<StudentEntity> enrolledStudentEntity = tge.getStudentEntityCollection();
////        for (int i = 0; i < enrolledStudentEntity.size(); i++)
////        {
////            StudentEntity se = enrolledStudentEntity.get(i);
////            enrolledStudents.add(se.getStudentObject());
////        }
////        return enrolledStudents;
//        return null;
//    }

//    public List<TutorialGroup> getEnrolledTutorialGroups(String id)
//    {
//        List<TutorialGroup> tgEnrolledCollection = new ArrayList();
//        List<TutorialGroup> tgCollection = getAllTutorialGroups();
//        for(int i=0; i<tgCollection.size(); i++)
//        {
//            TutorialGroup tg = tgCollection.get(i);
//            if(tg.getStudent(id) != null)
//            {
//                tgEnrolledCollection.add(tg);
//            }
//        }
//        return tgEnrolledCollection;
//    }

//    public void enrolStudent(String id, String subjectCode, Integer activityNo)
//    {
////        StudentEntity studentEntity = em.find(StudentEntity.class, id);
////        TutorialGroupPK pk = new TutorialGroupPK(activityNo, subjectCode);
////        TutorialGroupEntity tge = em.find(TutorialGroupEntity.class, pk);
////        tge.enrolStudent(studentEntity);
//    }

//    public List<Student> getAllEnrolledStudent(String subjectCode, Integer activityNo)
//    {
////        TutorialGroupPK pk = new TutorialGroupPK(activityNo, subjectCode);
////        TutorialGroupEntity tge = em.find(TutorialGroupEntity.class, pk);
////        List<Student> studentCollection = new ArrayList();
////        List<StudentEntity> studentEntityCollection = tge.getStudentEntityCollection();
////        for(int i=0; i<studentEntityCollection.size(); i++)
////        {
////            StudentEntity se = studentEntityCollection.get(i);
////            studentCollection.add(se.getStudentObject());
////        }
////        return studentCollection;
//        return null;
//    }
}
